<?php

namespace app\modules\mall\models;
use Yii;
use yii\db\ActiveRecord;
/**
 * 购物车
 * @author XT
 */
class Cart extends ActiveRecord {
    
    public static function db() {
        return Yii::$app->mall;
    }
    
    public static function tableName() {
        return 'yy_cart';
    }
    /**
     * 得到购物车信息
     * 未加分页
     * @param unknown $data
     */
    public function getCartinfo($data) {
        $sql = 'select * from ' . self::tableName() . ' where uid=' . $data['uid'];
        return self::db()->createCommand($sql)->queryAll();
    }
    /**
     * 将商品加入到购物车
     * @param unknown $data
     */
    public function addCart($data) {
        self::db()->createCommand()
        ->insert(self::tableName(), [
            'uid'               => $data['uid'],
            'goods_id'          => $data['goodsid'],
            'skuid'             => $data['skuid'],
            'shop_id'           => $data['shopid'],
            'shop_name'         => $data['shopname'],
            'num'               => $data['num']
        ])->execute();
        return self::db()->getLastInsertID();
    }
    /**
     * 更新购物车信息
     * @param unknown $data
     */
    public function updateCart($data) {
        $sql = 'update ' . self::tableName() . ' set num=num+' . $data['num'] . ' where uid=' . $data['uid'] . ' and goods_id=' . $data['goodsid'] . ' and skuid=' . $data['skuid'];
        return self::db()->createCommand($sql)->execute();
        /*
        return self::db()->createCommand()
        ->update(self::tableName(), [
            'num'               => $data['num']
        ],
        [
            'uid'               => $data['uid'],
            'goods_id'          => $data['goodsid'],
            'skuid'             => $data['skuid']
        ])->execute();*/
    }
    /**
     * 直接更新购物车数量
     */
    public function updateCartByNum($data) {
        return self::db()->createCommand()
        ->update(self::tableName(), [
            'num'               => $data['num']
        ],
        [
            'uid'               => $data['uid'],
            'goods_id'          => $data['goodsid'],
            'skuid'             => $data['skuid']
        ])->execute();
    }
    /**
     * 删除购物车商品
     * @param unknown $data
     */
    public function delCart($data) {
        $uid        = $data['uid'];
        $goods_id   = $data['goodsid'];
        $skuid      = $data['skuid'];
        return self::db()->createCommand()->delete(self::tableName(), 'uid=' . $uid . ' and goods_id=' . $goods_id . ' and skuid=' . $skuid)->execute();
    }
    /**
     * 判断购物车中是否有该商品
     * @param unknown $uid
     * @param unknown $goods_id
     * @param unknown $skuid
     */
    public function hasGoods($uid, $goods_id, $skuid) {
        $sql = 'select count(1) from ' . self::tableName() . ' where uid=' . $uid . ' and goods_id=' . $goods_id . ' and skuid=' . $skuid;
        return self::db()->createCommand($sql)->queryScalar();
    }
    /**
     * 得到购物车中的商店信息
     * @param unknown $data
     */
    public function getCartShopinfo($data) {
        $uid = $data['uid'];
//         $page = $data['page'];
//         $size = $data['size'];
//         $limit = ($page - 1) * $size;
//         $sql = 'select distinct uid, shop_id, shop_name from yy_cart where uid=' . $uid . ' limit ' . $limit . ' , ' . $size;
        $sql = 'select distinct uid, shop_id, shop_name from yy_cart where uid=' . $uid;
        return self::db()->createCommand($sql)->queryAll();
    }
    /**
     * 通过商店id和uid，得到用户在该商店的购物车信息
     * @param unknown $data
     */
    public function getCartinfoByshopid($data) {
        $sql = 'select * from ' . self::tableName() . ' where uid=' . $data['uid'] . ' and shop_id=' . $data['shop_id'];
        return self::db()->createCommand($sql)->queryAll();
    }
    /**
     * 通过uid得到购物车中商品数量
     * @param unknown $uid
     */
    public function getCartNum($uid) {
        $sql = 'select sum(num) from ' . self::tableName() . ' where uid=' . $uid;
        return self::db()->createCommand($sql)->queryScalar();
    }
}